package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.BranchType;
import com.mbc.web.common.client.ButtonActionType;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmBranchType extends Composite {
	
	@UiField MButton btnSave;
	@UiField MButton btnEdit;
	@UiField MButton btnDelete;
	@UiField MButton btnReset;
	@UiField MButton btnNew;
	@UiField MButton btnBack;
	@UiField MButton btnList;
	@UiField MTextBox txtCode;
	@UiField MTextBox txtName;
	@UiField MTextBox txtDescription;
	
	private long branchTypeID;
	private long branchID;
	private BranchType branchType;
	byte action_type;

	private static FrmBranchTypeUiBinder uiBinder = GWT
			.create(FrmBranchTypeUiBinder.class);

	interface FrmBranchTypeUiBinder extends UiBinder<Widget, FrmBranchType> {
	}

	public FrmBranchType() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		onBtnNewClick(null);
	}

	public FrmBranchType(BranchType branchType) {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		this.branchType = branchType;
		bindControls(branchType);
		enableControls(false);
		btnSave.setEnabled(false);
		btnEdit.setEnabled(true);
		btnDelete.setEnabled(true);
		btnReset.setEnabled(false);
	}
	private void initialize(){
		
		txtCode.getElement().setAttribute("style", "text-transform: uppercase;");
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				txtCode.setFocus(true);
			}
		});
		txtDescription.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if(l_txt == txtDescription)
						btnSave.setFocus(true);
				}
			}
		});
	}
	private void enableControls(boolean enabled){
		txtCode.setEnabled(enabled);
		txtName.setEnabled(enabled);
		txtDescription.setEnabled(enabled);
	}
	
	private void bindControls(BranchType branchType){
		txtCode.setText(branchType.getBranchTypeCode());
		txtName.setText(branchType.getName());
		txtDescription.setText(branchType.getDescription());
		branchTypeID = branchType.getBranchTypeId();
		branchID = branchType.getBranchID();
		
	}
	
	private native void jsClear()/*-{
	$wnd.$("input").val("");
	}-*/;
	
	private void clear(){
		jsClear();
//		ViewUtils.clearRequiredNotifications();
		branchTypeID = 0;
		branchType = null;
	}
	private BranchType bindBranchType(){
		branchType = new BranchType();
		branchType.setBranchTypeId(branchTypeID);
		branchType.setBranchTypeCode(txtCode.getText().toUpperCase());
		branchType.setName(txtName.getText());
		branchType.setDescription(txtDescription.getText());
		return branchType;
	}
	private AsyncCallback<Integer> actionCallback()
	{
		return new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if(result == HttpStatus.OK){
					switch (action_type) {
					case ButtonActionType.action_save:
						ViewUtils.showSuccessMessage(CommonMessage.saved_message);
						ClientSingleton.changeView(new SearchBranchType(txtCode.getText(), SearchBranchType.SAVE));
						break;
					case ButtonActionType.action_delete:
						ViewUtils.showSuccessMessage(CommonMessage.deleted_message);
						ClientSingleton.changeView(new SearchBranchType(txtCode.getText(), SearchBranchType.DELETE));
						break;
					}
				}
				else{
					if(result == HttpStatus.INTERNAL_SERVER_ERROR)
						ViewUtils.showErrorMessage(CommonMessage.internal_server_error_message);
					else if(result == HttpStatus.BAD_REQUEST)
						ViewUtils.showErrorMessage(CommonMessage.getAlreadyExistedMessage("Bank Code(" + txtCode.getText() + ")"));
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
			
				
			}
		};
	}
	@UiHandler("btnNew")
	void onBtnNewClick(ClickEvent event) {
		clear();
		enableControls(true);
		txtCode.setFocus(true);
		btnSave.setEnabled(true);
		btnEdit.setEnabled(false);
		btnDelete.setEnabled(false);
		btnReset.setEnabled(false);
	}
	@UiHandler("btnSave")
	void onBtnSaveClick(ClickEvent event) {
		if(ViewUtils.isValidate()){
			action_type = ButtonActionType.action_save;
			ViewUtils.startAjaxLoader(CommonMessage.saving_message);
			if(branchTypeID == 0)
				CommonControllerMgr.createBranchType(bindBranchType(), actionCallback());
			else
				CommonControllerMgr.updateBranchType(bindBranchType(), actionCallback());
		}
	}
	@UiHandler("btnEdit")
	void onBtnEditClick(ClickEvent event) {
		if(branchTypeID != 0)
		{
			btnReset.setEnabled(true);
			btnSave.setEnabled(true);
			btnEdit.setEnabled(false);
			btnNew.setEnabled(false);
			enableControls(true);
			txtName.setFocus(true);
//			txtDescription.setFocus(true);
			txtCode.setEnabled(false);
		}
	}
	@UiHandler("btnDelete")
	void onBtnDeleteClick(ClickEvent event) {
		if(branchTypeID != 0)
		{
			ViewUtils.showConfirm(CommonMessage.delete_confirm_message, new CloseHandler<PopupPanel>() {
				
				@Override
				public void onClose(CloseEvent<PopupPanel> event) {
					if(ViewUtils.isAlertYes()){
						action_type = ButtonActionType.action_delete;
						ViewUtils.startAjaxLoader(CommonMessage.deleting_message);
						CommonControllerMgr.deleteBranchType(branchTypeID, actionCallback());
					}
				}
			});
		}
	}
	@UiHandler("btnReset")
	void onBtnResetClick(ClickEvent event) {
		bindControls(branchType);
		enableControls(false);
		btnReset.setEnabled(false);
		btnSave.setEnabled(false);
		btnEdit.setEnabled(true);
		btnNew.setEnabled(true);
	}
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		ClientSingleton.changeView(new SearchBranchType());
	}
	@UiHandler("txtCode")
	void ontxtCodeChange(ChangeEvent event) {
		if (!txtCode.getText().equals("")) {
			BranchType l_info = new BranchType();
			l_info.setBranchTypeCode(txtCode.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchTypeByCode(l_info, getBranchTypeCallback());
		}
	}
	private AsyncCallback<BranchType> getBranchTypeCallback() {
		return new AsyncCallback<BranchType>() {

			@Override
			public void onSuccess(BranchType result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					branchType = result;
					bindControls(result);
					enableEditable(false);
					btnSave.setEnabled(false);
					btnEdit.setEnabled(true);
					btnDelete.setEnabled(true);
					btnEdit.setFocus(true);
				}
			}

			private void enableEditable(boolean b) {
				txtCode.setEnabled(b);
				txtDescription.setEnabled(b);
				txtName.setEnabled(b);
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		};
	}
	@UiHandler("btnList")
	void onbtnListClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmBranchTypeRpt());
	}
	
}
